package net.kts.subskill.tel.embassy;

import java.util.ArrayList;
import java.util.List;

import net.kts.main.R;
import net.kts.transrate.TranslateService;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/*
 * 대사관 전화목록을 불러오는 액티비티
 */
public class Embassy extends ListActivity {
	private ProgressDialog mProgress;
	private EmbassyAdapter embAdapter;
	private List<EmbassyVO> embassyList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tel_embassy_call);
        mProgress		= new ProgressDialog(this);
        embAdapter 		= new EmbassyAdapter(this);
        embassyList 	= new ArrayList<EmbassyVO>();

    	// null이거나 공백일 경우, 디폴트 텍스트 출력
        String strEmbassy = TranslateService.translateRepository("Embassy");
        if(strEmbassy == null){
        }else if(strEmbassy.trim().equals("")){
        }else{
            TextView tvLabel = (TextView) findViewById(R.id.tv_emb_Label);
            tvLabel.setText(strEmbassy);
        }
        
        loadList();
        
    }
    
    public void loadList(){
    	
		String wait = TranslateService.translateRepository("잠시만 기다려주세요.");
		if(wait == null){
			mProgress.setMessage("Please wait a moment.");
		}else if(wait.trim().equals("")){
			mProgress.setMessage("Please wait a moment.");
		}else{
			mProgress.setMessage(wait);
		}
    	mProgress.show();

    	new Thread() {
    		@Override
    		public void run() {
    			int what = 0;
    			
    			try {
    				Serializer serializer = new Persister();
    				try {
    					// 대사관 전화 xml 리스트
    					EmbassyXml readEmbassyList = serializer.read(EmbassyXml.class, getResources().openRawResource(R.raw.call_embassy));
    					embassyList = readEmbassyList.getList();
    					
    					for (EmbassyVO vo : embassyList) {
							vo.setName(vo.getName());
						}
    					
    					if(embassyList.size() < 1){
    						Log.i("tel333", "대사관 리스트가 0");
    						finish();

    					}

    				} catch (NotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			} catch (Exception e) {
    				Log.i("log333", "loadList Exception");
    				what = 1;
    				e.printStackTrace();
    			}
    			
    			mHandler.sendMessage(mHandler.obtainMessage(what));
    		}
    	}.start();

    }
    
    private Handler mHandler = new Handler() {
    	
    	@Override
    	public void handleMessage(Message msg) {
    		mProgress.dismiss();
    		
    		if (msg.what == 0) {
    			if (embassyList.size() == 0) {
    				Toast.makeText(Embassy.this, "No embassy list available", Toast.LENGTH_SHORT).show();
    				return;
    			}
				// 출력
				embAdapter.setData((ArrayList<EmbassyVO>) embassyList);
				setListAdapter(embAdapter);
    			
    		} else {
    			Toast.makeText(Embassy.this, "Failed to load  embassy list", Toast.LENGTH_SHORT).show();
    		}
    	}
    };
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		EmbassyVO item = (EmbassyVO) l.getItemAtPosition(position);
		
		Log.e("kts", "Tel : "+item.getTel());
		startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
											+ item.getTel())));
	}

}
